/**
 * informationController Module
 *
 * InformationController
 */
angular.module('app.con.information', []).controller('InformationController', function($scope, $modal, InformationService) {
    // ngView部分高度自适应
    var window_height = window.innerHeight - document.getElementsByTagName('nav')[0].offsetHeight - document.getElementsByTagName('footer')[0].offsetHeight;
    document.getElementById('ngView').style.height = window_height + 'px';

    // datepicker实现函数，实现选择日期控件
    $scope.today = function() {
        $scope.dt = new Date(); // 定义一个属性来接收当天日期
    };
    $scope.today();
    $scope.clear = function() {
        $scope.dt = null;
    }
    $scope.open = function($event) {
        $event.preventDefault();
        $event.stopPropagation();
        $scope.opened = true;
    }
    $scope.disabled = function(date, mode) {
        return (mode === 'day' && (date.getDay() === 0 || date.getDay() === 6))
    }
    $scope.toggleMin = function() {
        $scope.minDate = $scope.minDate ? null : new Date();
    }
    $scope.toggleMin();
    $scope.dateOptions = {
        formatYear: 'yy',
        startingDay: 1
    }
    $scope.formats = ['dd-MMMM-yyyy', 'yyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
    $scope.format = $scope.formats[0];

    //信息展示
    InformationService.infoList().success(function(res) {
        $scope.customers = res;
    });

    //购买记录
    $scope.shoppingList = function(name_id, email) {
        InformationService.purchaseHistoryList({ name_id: name_id, email: email }).success(function(res) {
            $scope.list = res;

            var shoppingListModule = $modal.open({
                templateUrl: 'shoppingList.html',
                controller: 'shoppingListController',
                resolve: {
                    list: function() {
                        return $scope.list;
                    }
                }
            });
        });
    };

    //修改客户信息
    $scope.editInformation = function(customer) {
        $scope.customer = customer;
        var editInformationModule = $modal.open({
            templateUrl: 'editInformation.html',
            controller: 'EditInformationController',
            resolve: {
                customer: function() {
                    return $scope.customer;
                }
            }
        });
        editInformationModule.result.then(function(result) {
            customer.name        = result.name;
            customer.email       = result.email;
            customer.phone       = result.phone;
            customer.address     = result.address;
            customer.recommended = result.recommended;
            customer.source      = result.source;
            customer.remarks     = result.remarks;
        }, function(reason) {
            console.log(reason);
        });
    };
});

//购买记录
angular.module('app.con.information.shoppingList', []).controller('shoppingListController', function($scope, list) {
    $scope.shopping_lists = list.shopping_list;
});

//修改客户信息
angular.module('app.con.information.editInformation', []).controller('EditInformationController', function($scope, $modalInstance, customer, InformationService) {
    $scope.customer = customer;

    //表单自动填充
    $scope.editCustomerName        = customer.name;
    $scope.editCustomerEmail       = customer.email;
    $scope.editCustomerPhone       = customer.phone;
    $scope.editCustomerAddress     = customer.address;
    $scope.editCustomerRecommended = customer.recommended;
    $scope.editCustomerSource      = customer.source;
    $scope.editCustomerRemarks     = customer.remarks;

    //表单验证
    $scope.checkEditCustomerName = function() {
        if ($scope.editCustomerName) {
            $scope.editCustomerNameStyle = false;
        } else {
            $scope.editCustomerNameStyle = true;
        };
    };
    $scope.checkEditCustomerEmail = function() {
        if ($scope.editCustomerEmail) {
            $scope.editCustomerEmailStyle = false;
        } else {
            $scope.editCustomerEmailStyle = true;
        };
    };

    //表单提交
    $scope.editCustomer = function() {
        if ($scope.editCustomerName && $scope.editCustomerEmail) {
            var customer = {
                id         : $scope.customer.id,
                name       : $scope.editCustomerName,
                email      : $scope.editCustomerEmail,
                phone      : $scope.editCustomerPhone,
                address    : $scope.editCustomerAddress,
                recommended: $scope.editCustomerRecommended,
                source     : $scope.editCustomerSource,
                remarks    : $scope.editCustomerRemarks
            };
            $modalInstance.close(customer);
            InformationService.modify(customer).success(function(res) {
                console.log(res);
            });
        } else {
            return false;
        };
    };
    
});
